#include <stdio.h>
#include <time.h>
#include<sys/time.h>
#include<stdlib.h>
#include<omp.h>
int main()
{
	int i,j,m,n,a,b,h,sum;
	double time1,time2;
	struct timeval start;
	struct timeval end;
	sum=0;
	double speedup;
	for(m=100;m<=4096;)
	{
		double M[m][m],N[m][m],K[m][m];
		i=j=n=m;
		for(a = 0 ; a < m ; a++){
			for(b = 0 ; b < n ; b++){
				M[a][b]=N[a][b]=rand()%10+1;
			}
		} 	
		gettimeofday(&start,NULL);
 # pragma omp parallel for default(none) shared(K,M,n) private(h,b,a,sum)  num_threads(4) 
		for(h=0;h<n;h++)
		{

			for(b=0;b<n;b++)
			{

				for(a=0;a<n;a++)
				{
					sum=sum + M[h][a]*M[a][b];
					K[h][b]=sum;
				}
				sum=0;
			}
		}

		gettimeofday(&end,NULL);
		time1=end.tv_usec-start.tv_usec;
		printf("四线程常规矩阵乘法所需时间：%f ",time1);
		gettimeofday(&start,NULL);
# pragma omp parallel for default(none) shared(K,N,n) private(h,b,a,sum) num_threads(1)
		for(h=0;h<n;h++)
		{

			for(b=0;b<n;b++)
			{

				for(a=0;a<n;a++)
				{
					sum=sum + N[h][a]*N[a][b];
					K[h][b]=sum;
				}
				sum=0;
			}
		}

		gettimeofday(&end,NULL);
		time2=end.tv_usec-start.tv_usec;
		printf("单线程常规矩阵乘法所需时间：%f ",time2);
		m=m+16;
		speedup = time2/time1;
		printf("加速比为%f\n",speedup);
	}
	
	return 0;
}
